package com.example.hellodemo.pageView2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.CompositePageTransformer;
import androidx.viewpager2.widget.MarginPageTransformer;
import androidx.viewpager2.widget.ViewPager2;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.hellodemo.R;

import java.util.ArrayList;
import java.util.List;

public class ViewPage_RecycleAdapter extends AppCompatActivity {
    private ViewPager2 viewPager_one, viewPager_two, viewPager_three;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_page__recycle_adapter);

        /*正常左右滑动*/
        configViewPage_one();
        /*上下滑动*/
        configViewPage_two();
        configViewPage_three();


    }



    private void configViewPage_one() {
        viewPager_one = findViewById(R.id.viewPage_one);
        viewPager_one.setAdapter(myRecycleViewAdapter);

       //TODO 滑动事件监听
        viewPager_one.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);

                Toast.makeText(getApplicationContext(), "page selected" + position + "position", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);

            }
        });

        //TODO 禁止用户滑动


//        viewPager_one.setUserInputEnabled(false);

        //TODO 模拟拖拽
       viewPager_one.fakeDragBy(-310f);

       //TODO  PageTransformer配置页面效果,   如果想配置多种效果 则通过 CompositePageTransformer 组合PageTransformer

        //1. 页面间距
//        viewPager_one.setPageTransformer(new MarginPageTransformer(40));

//        2. CompositePageTransformer为ViewPager设置了MarginPageTransformer和一个页面缩放的ScaleInTransformer。
        CompositePageTransformer compositePageTransformer = new CompositePageTransformer();
        compositePageTransformer.addTransformer(new ScaleInTransformer());
        compositePageTransformer.addTransformer(new MarginPageTransformer(40));
        viewPager_one.setPageTransformer(compositePageTransformer);

       //3  一屏多页效果
//        RecyclerView recyclerView = (RecyclerView)viewPager_one.getChildAt(0);
//        int padding = 40 + 40;
//        recyclerView.setPadding(padding,0,padding,0);
//        recyclerView.setClipToPadding(false);

//        CompositePageTransformer compositePageTransformer = new CompositePageTransformer();
//        compositePageTransformer.addTransformer(new ScaleInTransformer());
//        compositePageTransformer.addTransformer(new MarginPageTransformer(40));
//        viewPager_one.setPageTransformer(compositePageTransformer);


    }

    private void configViewPage_two() {
        viewPager_two = findViewById(R.id.viewPage_two);
        viewPager_two.setAdapter(myRecycleViewAdapter);
        viewPager_two.setOrientation(ViewPager2.ORIENTATION_VERTICAL);

    }
    private void configViewPage_three() {
        viewPager_three = findViewById(R.id.viewPage_three);
    }


    private RecyclerView.Adapter myRecycleViewAdapter = new RecyclerView.Adapter() {
        private  final String[] Images = {"#CCFF99","#41F1E5","#8D41F1","#FF99CC"};
        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

            ImageView imageView = new ImageView(getApplicationContext());
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            return new MyViewHolder(imageView);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            ImageView imageView = (ImageView) holder.itemView;
            imageView.setBackgroundColor(Color.parseColor(Images[position]));

        }

        @Override
        public int getItemCount() {
            return Images.length;
        }

        class  MyViewHolder extends RecyclerView.ViewHolder{

            public MyViewHolder(@NonNull View itemView) {
                super(itemView);

            }
        }
    };


   class  ScaleInTransformer implements ViewPager2.PageTransformer {
       private final  float  DEFAULT_MIN_SCALE = 0.85f;
       private final  float  DEFAULT_CENTER = 0.5f;
       private final  float mMinScale = DEFAULT_MIN_SCALE;

       @Override
       public void transformPage(@NonNull View page, float position) {
            page.setElevation(- Math.abs(position));
            float pageWidth = page.getWidth();
            float pageHeight = page.getHeight();

            page.setPivotX(pageHeight / 2);
            page.setPivotX(pageWidth/2);
            if (position < -1){
                page.setScaleX(mMinScale);
                page.setScaleY(mMinScale);
                page.setPivotX(pageWidth);
            }else  if(position <= 1){
                if (position < 0){
                    float scaleFactor = (1 + position) * (1 - mMinScale) + mMinScale;
                    page.setScaleX(scaleFactor);
                    page.setScaleY(scaleFactor);
                    page.setPivotX(pageWidth * (DEFAULT_CENTER + DEFAULT_CENTER * -position));
                }else {
                    float scaleFactor = (1 - position) * (1 - mMinScale) + mMinScale;
                    page.setScaleX(scaleFactor);
                    page.setScaleY(scaleFactor);
                    page.setPivotX(pageWidth * (1 -position) * DEFAULT_CENTER);
                }
            }else {
                page.setPivotX(0);
                page.setScaleX(mMinScale);
                page.setScaleY(mMinScale);
            }


       }
   }


}
